home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: Problem with scanf
- Date: 21 Mar 1996 15:12 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <21MAR199615124123@erich.triumf.ca>
- References: <4iscgb$8h9@newsflash.concordia.ca>
- NNTP-Posting-Host: ftp.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4iscgb$8h9@newsflash.concordia.ca>, v_ruso@alcor.concordia.ca writes...
- >I am writing a program that is supposed to gather information from the
- >user.
- >The program works fine if the user enters some numbers and presses
- >enter. But if the user just presses enter without entering any data,
- >the cursor just starts a new line.
- >Can anyone tell me how I can avoid this?
- >this is the part of the code i am refering to:
- >..
- >..
- >gotoxy (13,22);
- > cprintf ("The maximum load the cylinder is expected to handle");
- > gotoxy (15,10);
- > cprintf ("Enter the Maximum Axial Load [Lbf]: ");
- > xlocation = wherex();
- > gotoxy (xlocation,10);
- > scanf ("%f",&maxload);
-
- scanf() is very nasty for direct user input if the user enters "unexpected"
- things. The usual recommendation is to get the user's input with fgets(), then
- parse it with sscanf(), or other things, as needed. You can then easily check
- for valid input, and recover from errors.
-
- However, since you are using the DOS-specific (and non-portable) cprintf()
- and gotoxy(), you should use a corresponding input function - namely cgets().
- Read the description on cgets() _very_ carefully - it is a bit strange!!
- Before you call it, you set the first char in its buffer to the maximun number
- of chars to get, then it sets the second char to the number it got, and the
- string actually starts in the third position (buffer[2]).
-
- This all applies _only_ to Borland compilers - Microsoft may have a different
- input function (I know they call "gotoxy()" something else...)
-
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
- or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
-
-